package com.erp.myAdapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.erp.bean.ChedingdanBean
import com.erp.R
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.lv_che_danju.view.*
import java.util.*

/**
 * Created by Administrator on 2017/2/10/010.
 * 作者：刘洋
 */
class ChedingdanAdapter(private val mcontext: Context, private var mlist: List<ChedingdanBean>) : BaseAdapter() {
    private val minflater: LayoutInflater= LayoutInflater.from(mcontext)
    /**
     * 被选中的订单
     */
    private val listoid: MutableList<String> = ArrayList()
    private val listcar: MutableList<String> = ArrayList()

    override fun getCount(): Int {
        return mlist.size
    }

    override fun getItem(position: Int): ChedingdanBean {
        return mlist[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    @SuppressLint("ViewHolder")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var v = minflater.inflate(R.layout.lv_che_danju, null)
        val chedingdanBean = mlist[position]
        Myutil.settv(chedingdanBean.contract_num, v.TvContractNum)
        Myutil.settv(chedingdanBean.deliver_car, v.TvDeliverCar)
        Myutil.settv(chedingdanBean.onum, v.TvOnum)
        Myutil.settv(chedingdanBean.shop_name, v.TvShopName)
        Myutil.settv(chedingdanBean.deliver_car_number, v.TvDeliverCarNumber)
        if (!istrue(chedingdanBean.oid)) {
            v.CbSon.isChecked = false
        }
        v.CbSon.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                listoid.add(chedingdanBean.oid)
                listcar.add(chedingdanBean.deliver_car_number)
            } else {
                listoid.remove(chedingdanBean.oid)
                listcar.remove(chedingdanBean.deliver_car_number)
            }
        }
        return v
    }

    fun getlistoid(): List<String> {
        return listoid
    }

    fun getlistcar(): List<String> {
        return listcar
    }

    fun cleanisb() {
        listoid.clear()
        listcar.clear()
    }

    fun addisb() {
        for (i in mlist.indices) {
            listoid.add(mlist[i].oid)
            listcar.add(mlist[i].deliver_car_number)
        }
    }

    fun Allnotify(list: List<ChedingdanBean>) {
        this.mlist = list
        addisb()
        notifyDataSetChanged()
    }

    fun istrue(oid: String): Boolean {
        for (i in listoid.indices) {
            if (oid == listoid[i]) {
                return true
            }
        }
        return false
    }
}
